<?php
namespace Home\Controller;
use Think\Controller;
class JieController extends CommonController {

	public function index()
	{

		$id = I('get.id');

        //问题数据
        $sql ="select t1.*,t2.nickname,t2.face from question t1 inner join user t2 on t1.uid=t2.id where t1.id='{$id}'";
        
        $res = M()->query($sql);

        $question = current($res);

        // 增加浏览量
        $this->autoAdd('question','view_num',"+1",$id);

        //获取hot
        $this->getTopView();
        $this->getTopAnswer();


        $sql = "select t1.*,t2.nickname,t2.face from answer t1 inner join user t2 on t1.uid=t2.id where t1.qid='{$id}'";

        $answers = M()->query($sql);

        $this->assign('answers',$answers);
		$this->assign('question',$question);

        $this->assign('title',$question['title']);

		$this->display();
	}
    public function add(){
    	//获取随机一条人类验证
    	$question = D('verify')->getOneVerify();

    	$this->assign('question',$question);

    	$cates = D('category')->getCate();
    	$this->assign('cates',$cates);
        $this->display();
    }
    public function ajaxAdd()
    {

    	$verify = I('post.verify');

        // 判断人类验证是否正确
    	if(md5($verify) != $_SESSION['answer']){
    		$arr = array(
    			'error'=>1,
    			'info'=>'人类验证不通过'
    			);
    		echo json_encode($arr);
    		exit;
    	}

        // $this->autoAdd('user','kiss',-20);

    	$data = I('post.');

    	$data['uid']=$_SESSION['id'];

    	$data['create_time'] =time();

    	if($id = M('question')->add($data)){
    		$arr = array(
    			'error'=>0,
    			'info'=>'发布成功',
    			'url'=>U('home/jie/index',array('id'=>$id))
    			);
    		echo json_encode($arr);
    		exit;
    	}else{
    		$arr = array(
    			'error'=>1,
    			'info'=>'发布失败'
    			);
    		echo json_encode($arr);
    		exit;
    	}
    	
    }

    public function uploadImage()
    {
    	$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Pic/'; // 设置附件上传根目录
	    $upload->savePath  =     ''; // 设置附件上传（子）目录
	    // 上传文件 
	    $info   =   $upload->upload();

	    if(!$info) {// 上传错误提示错误信息
	        echo $upload->getError();
	    }else{// 上传成功
	    	$info = current($info);
	    	$path = __ROOT__."/Pic/".$info['savepath'].$info['savename'];

	    	$arr = array(
	    		'code'=>0,
	    		'msg'=>'成功',
	    		'data'=>array(
	    			'src'=>$path,
	    			'title'=>'图片',
	    			)
	    		);
	    	echo json_encode($arr);
	    }
    }

    public function addAnswer()
    {
        $data = I('post.');
        $data['uid']=$_SESSION['id'];
        $data['create_time'] = time();

        if(M('answer')->add($data)){
            $this->autoAdd('question','answer_num',"+1",I('post.qid'));
            $this->autoAdd('user','answer_num',"+1",$_SESSION['id']);
            $arr = array(
                'error'=>0,
                'info'=>'回复成功'
                );
            echo json_encode($arr);
            exit;
        }else{
            $arr = array(
                'error'=>1,
                'info'=>'回复失败'
                );
            echo json_encode($arr);
            exit;
        }
    }
    public function So()
    {
        header("Content-type: text/html; charset=utf-8");
        $q = I('get.q');
        Vendor('Pscws4.pscws4','','.class.php');
        $pscws = new \PSCWS4('utf8');

        $pscws->set_dict('./Public/dict.utf8.xdb');

        $pscws->send_text($q);

        $tops = $pscws->get_tops(3, 'n,v');

        $sql = "select * from question where ";

        foreach ($tops as $row) {
            $sql .= " title like '%".$row['word']."%' or";
        }

        $sql =  rtrim($sql,'or');


        $res = M()->query($sql);

        // str_replace('小明', "<span sytle='color:red'>小明</span>", )

        
    }
}